package com.example.SearchAround_JC.jc_manager;

import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2014/11/27.
 */
public class PoiResult {
    public List<PoiDetails> ResultPoi(String coordinate, String q, String range, int count) throws IOException, JSONException {
        List<PoiDetails> list = new ArrayList<PoiDetails>();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("coordinate", coordinate));
        params.add(new BasicNameValuePair("q", q));
        params.add(new BasicNameValuePair("range", range));
        params.add(new BasicNameValuePair("count",count +""));
        params.add(new BasicNameValuePair("access_token", "2.00AEMTsFI8k_KEe41fe2200089dCuC"));

        String paramString = URLEncodedUtils.format(params,"UTF-8");

        HttpGet httpGet = new HttpGet("https://api.weibo.com/2/location/pois/search/by_geo.json?" + paramString);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream inputStream = httpEntity.getContent();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            byteArrayOutputStream.write(buffer, 0, len);
        }
        String result = byteArrayOutputStream.toString();
        Log.d("result",result);

        JSONObject jsonObject = new JSONObject(result);
        JSONArray poilistJSONArray = jsonObject.getJSONArray("poilist");

        for (int i = 0;i < poilistJSONArray.length();i++){
            JSONObject object = poilistJSONArray.getJSONObject(i);
            String name = object.optString("name");
            Log.d("list_name",i+":"+name);
            String tel = object.optString("tel");
            Log.d("list_tel",i+":"+tel);
            String address = object.optString("address");
            Log.d("list_address",i+":"+address);
            String distance = object.optString("distance");
            Log.d("list_distance",i+":"+distance);
            String x = object.optString("x");
            Log.d("list_x",i+":"+x);
            String y = object.optString("y");
            Log.d("list_y",i+":"+y);
            PoiDetails poiDetails = new PoiDetails(name,tel,address,distance,x,y);
            list.add(poiDetails);
        }
        return list;
    }
}
